#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null


case "$1" in
 -buildlist)
 [ -f "$HOME/.config/tmp/servicelist" ] && rm -f $HOME/.config/tmp/servicelist
 # Each service installs a start/stop/restart script in /etc/init.d
 SERVICES="`ls /etc/init.d/`"
 for SERVICE in $SERVICES
 do
  # /etc/rc.d/rc.services is a plain text list of services to start at boot
  BOOT="`cat /etc/rc.d/rc.services | grep -v '#' | grep $SERVICE`"
  if [ ! "$BOOT" = "" ];then
   START="$LOC_ENABLED"
  else
   START="$LOC_DISABLED"
  fi
  # Each service should have a description and tooltip (which can be longer)
  DESC="`cat /etc/init.d/$SERVICE | grep '#DESC' | cut -f 2 -d '"'`"

  /etc/init.d/$SERVICE status > /dev/null
  if [ $? -eq 0 ];then
   STATUS="gtk-yes"
  else
   STATUS="gtk-no"
  fi

  echo "$STATUS|$SERVICE|$START|$DESC" >> $HOME/.config/tmp/servicelist
 done
 ;;
 
 #=======================================================
 -bootstart)
 if [ "`cat /etc/rc.d/rc.services | grep $2`" = "" ];then
  echo $2 >> /etc/rc.d/rc.services
 fi
 echo "$2 $LOC_BOOTSTART" > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -bootstop)
 cp -f /etc/rc.d/rc.services /etc/rc.d/rc.services.bak
 cat /etc/rc.d/rc.services.bak | grep -v $2 > /etc/rc.d/rc.services
 echo "$2 $LOC_BOOTSTOP" > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -start)
 sh /etc/init.d/$2 start
 echo "$2 $LOC_STARTED" > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -restart)
 sh /etc/init.d/$2 restart
 echo "$2 $LOC_RESTARTED" > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -stop)
 sh /etc/init.d/$2 stop
 echo "$2 $LOC_STOPPED" > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -info)
 SERVICE="$2"
 TTIP="`cat /etc/init.d/$SERVICE | grep '#TTIP' | cut -f 2 -d '"'`"
 echo $TTIP > $HOME/.config/tmp/infotext
 ;;
 
 #=======================================================
 -help)
 export HELP_DLG="
 <window title=\"\" icon_name=\"gtk-help\">
  <vbox>
   <text use_markup=\"true\">
    <label>\"<b>*****${LOC_MAIN}*****</b>\"</label>
   </text>
   <frame $LOC_USAGE>
    <text>
     <label>\"$LOC_HELP_TEXT\"</label>
    </text>
   </frame>
   <hbox>
    <button>
     <input file stock=\"gtk-close\"></input>
     <label>$LOC_CLOSE</label>
    </button>
   </hbox>
  </vbox>
 </window>
 "
 gtkdialog3 --program HELP_DLG
 ;;
esac








